package com.huawei.esdk.uc.sametime.preferences;

import org.apache.log4j.Logger;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.huawei.esdk.uc.jna.util.StringUtil;
import com.huawei.esdk.uc.sametime.ConstantsDefines;
import com.huawei.esdk.uc.sametime.EsdkPluginActivator;
import com.huawei.esdk.uc.sametime.international.Messages;

/**
 * This class represents a preference page that is contributed to the
 * Preferences dialog. By subclassing <samp>FieldEditorPreferencePage</samp>, we
 * can use the field support built into JFace that allows us to create a page
 * that is small and knows how to save, restore and apply itself.
 * <p>
 * This page is used to modify preferences only. They are stored in the
 * preference store that belongs to the main plug-in class. That way,
 * preferences can be accessed directly via the preference store.
 */

public class UCAccountSubPreferencePage extends FieldEditorPreferencePage
        implements IWorkbenchPreferencePage
{
    private static Logger logger = Logger
            .getLogger(UCAccountSubPreferencePage.class);

    private StringFieldEditor userName;

    private StringFieldEditor pwd;

    private StringFieldEditor ucSvrIp;

    private IntegerFieldEditor port;

    public UCAccountSubPreferencePage()
    {
        super(GRID);
        setPreferenceStore(EsdkPluginActivator.getDefault()
                .getPreferenceStore());
        setDescription(Messages.UCAccountSubPreferencePage_DESCRIPTION);

        logger.debug(getDescription());
    }

    /**
     * Creates the field editors. Field editors are abstractions of the common
     * GUI blocks needed to manipulate various types of preferences. Each field
     * editor knows how to save and restore itself.
     */
    public void createFieldEditors()
    {
        userName = new StringFieldEditor(PreferenceConstants.P_USERNAME,
                Messages.UCAccountSubPreferencePage_ACCOUNT,
                getFieldEditorParent());
        pwd = new StringFieldEditor(PreferenceConstants.P_PASSWORD,
                Messages.UCAccountSubPreferencePage_PASSWORD,
                getFieldEditorParent());
        ucSvrIp = new StringFieldEditor(PreferenceConstants.P_UCSVRIP,
                Messages.UCAccountSubPreferencePage_SERVER_ADDRESS,
                getFieldEditorParent());
        port = new IntegerFieldEditor(PreferenceConstants.P_PORT,
                Messages.UCAccountSubPreferencePage_SERVER_PORT,
                getFieldEditorParent());

        /*
         * 密码字符串用"*"代替
         */
        pwd.getTextControl(getFieldEditorParent()).setEchoChar('*');

        addField(userName);
        addField(pwd);
        addField(ucSvrIp);
        addField(port);

        adjustGridLayout();

    }

    /**
     * 提交“确定”或“应用”按钮，覆盖FieldEditorPreferencePage.performOk方法
     */
    public boolean performOk()
    {
        // 调用super.performok会导致将有错误的值保存到起来
        // super.performOk();
        boolean result = true;
        String userName_value = userName.getStringValue();
        String pwd_value = pwd.getStringValue();
        String ucSvrIp_value = ucSvrIp.getStringValue();
        int port_value = port.getIntValue();
        boolean hasError = false;

        if (StringUtil.isEmptyStr(ucSvrIp_value))
        {
            setErrorMessage(Messages.UCAccountSubPreferencePage_ERROR_EMPTY_SERVER_ADDRESS);
            result = false;
            hasError = true;
        }
        if (!hasError
                && (ucSvrIp_value.contains(ConstantsDefines.SPACE_CHAR_STR)))
        {
            setErrorMessage(Messages.UCAccountSubPreferencePage_ERROR_SERVER_ADDRESS_HAS_SPACE);
            result = false;
            hasError = true;
        }
        if (!hasError && StringUtil.isEmptyStr(pwd_value))
        {
            setErrorMessage(Messages.UCAccountSubPreferencePage_ERROR_PASSWORD_EMPTY);
            result = false;
            hasError = true;
        }
        if (!hasError && pwd_value.contains(ConstantsDefines.SPACE_CHAR_STR))
        {
            setErrorMessage(Messages.UCAccountSubPreferencePage_ERROR_PASSWORD_HAS_SPACE);
            result = false;
            hasError = true;
        }
        if (!hasError && StringUtil.isEmptyStr(userName_value))
        {
            setErrorMessage(Messages.UCAccountSubPreferencePage_ERROR_ACCOUNT_EMPTY);
            result = false;
            hasError = true;
        }
        if (!hasError && port_value > 65535)
        {
            setErrorMessage(Messages.UCAccountSubPreferencePage_PORT_ERROR_TIP);
            result = false;
            hasError = true;
        }
        if (result)
        {
            super.performOk();
            setMessage(Messages.UCAccountSubPreferencePage_MSG_ACCOUNT_SAVED);
        }
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
     */
    public void init(IWorkbench workbench)
    {
        logger.debug("UCAccountSubPreferencePage init");
    }

}